﻿using System;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using OpenGauss.NET.BackendMessages;
using OpenGauss.NET.PostgresTypes;

namespace OpenGauss.NET.Internal.TypeHandling
{
    /// <summary>
    /// Base class for all simple type handlers, which read and write short, non-arbitrary lengthed
    /// values to PostgreSQL. Provides a simpler API to implement when compared to <see cref="OpenGaussTypeHandler"/> -
    /// OpenGauss takes care of all I/O before calling into this type, so no I/O needs to be performed by it.
    /// </summary>
    /// <typeparam name="TDefault">
    /// The default CLR type that this handler will read and write. For example, calling <see cref="DbDataReader.GetValue"/>
    /// on a column with this handler will return a value with type <typeparamref name="TDefault"/>.
    /// Type handlers can support additional types by implementing <see cref="IOpenGaussTypeHandler{T}"/>.
    /// </typeparam>
    public abstract class OpenGaussSimpleTypeHandler<TDefault> : OpenGaussTypeHandler<TDefault>, IOpenGaussSimpleTypeHandler<TDefault>
    {
        protected OpenGaussSimpleTypeHandler(PostgresType postgresType) : base(postgresType) {}

        /// <summary>
        /// Reads a value of type <typeparamref name="TDefault"/> with the given length from the provided buffer,
        /// with the assumption that it is entirely present in the provided memory buffer and no I/O will be
        /// required.
        /// </summary>
        /// <param name="buf">The buffer from which to read.</param>
        /// <param name="len">The byte length of the value. The buffer might not contain the full length, requiring I/O to be performed.</param>
        /// <param name="fieldDescription">Additional PostgreSQL information about the type, such as the length in varchar(30).</param>
        /// <returns>The fully-read value.</returns>
        public abstract TDefault Read(OpenGaussReadBuffer buf, int len, FieldDescription? fieldDescription = null);

        public sealed override ValueTask<TDefault> Read(OpenGaussReadBuffer buf, int len, bool async, FieldDescription? fieldDescription = null)
            => throw new NotSupportedException();

        #region Write

        /// <summary>
        /// Responsible for validating that a value represents a value of the correct and which can be
        /// written for PostgreSQL - if the value cannot be written for any reason, an exception shold be thrown.
        /// Also returns the byte length needed to write the value.
        /// </summary>
        /// <param name="value">The value to be written to PostgreSQL</param>
        /// <param name="parameter">
        /// The <see cref="OpenGaussParameter"/> instance where this value resides. Can be used to access additional
        /// information relevant to the write process (e.g. <see cref="OpenGaussParameter.Size"/>).
        /// </param>
        /// <returns>The number of bytes required to write the value.</returns>
        public abstract int ValidateAndGetLength(TDefault value, OpenGaussParameter? parameter);

        /// <summary>
        /// Writes a value to the provided buffer, with the assumption that there is enough space in the buffer
        /// (no I/O will occur). The OpenGauss core will have taken care of that.
        /// </summary>
        /// <param name="value">The value to write.</param>
        /// <param name="buf">The buffer to which to write.</param>
        /// <param name="parameter">
        /// The <see cref="OpenGaussParameter"/> instance where this value resides. Can be used to access additional
        /// information relevant to the write process (e.g. <see cref="OpenGaussParameter.Size"/>).
        /// </param>
        public abstract void Write(TDefault value, OpenGaussWriteBuffer buf, OpenGaussParameter? parameter);

        /// <summary>
        /// Simple type handlers override <see cref="Write(TDefault,OpenGaussWriteBuffer,OpenGaussParameter)"/> instead of this.
        /// </summary>
        public sealed override Task Write(TDefault value, OpenGaussWriteBuffer buf, OpenGaussLengthCache? lengthCache, OpenGaussParameter? parameter, bool async, CancellationToken cancellationToken = default)
            => throw new NotSupportedException();

        /// <summary>
        /// Simple type handlers override <see cref="ValidateAndGetLength(TDefault,OpenGaussParameter)"/> instead of this.
        /// </summary>
        public sealed override int ValidateAndGetLength(TDefault value, ref OpenGaussLengthCache? lengthCache, OpenGaussParameter? parameter)
            => throw new NotSupportedException();

        #endregion
    }
}
